<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>

<body>
    <script>
        var url = 'http://www.nowcoder.com?key=1&key=2&key=3&test=4#hehe';
        console.log(getUrlParam(url, 'key'));
        console.log(getUrlParam(url));
        console.log(getUrlParam(url,'test'));
        console.log(getUrlParam(url,'test1'));

        function getUrlParam(sUrl, sKey) {
            var data = sUrl.split('?')[1];
            if(!data) return {};
            var reg = /([a-zA-Z]+)=(\w*)&?/g;
            var obj = {};
            data = data.replace(reg,function(s,g1,g2){
                if(g1 in obj){
                    obj[g1].push(g2);
                }else {
                    obj[g1] = [g2];
                }
                return '';
            });
            if (sKey) {
                if (sKey in obj) {
                    return obj[sKey].length>1 ? obj[sKey] : obj[sKey][0];
                } else {
                    return '';
                }
            } else {
                return obj;
            }
        }
    </script>
</body>

</html>